package com.aaa.controller;


import cn.hutool.core.map.MapUtil;
import com.aaa.entity.MyReturn;
import com.aaa.entity.Tester;
import com.aaa.service.TestResultFpaService;
import com.aaa.service.TesterFPAService;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yudejie2020
 */
@Controller
@RequestMapping("resultFpa")
public class TestResultFpaController extends BaseController{
    MyReturn myReturn=new MyReturn();
    @Autowired
    private TestResultFpaService testResultFpaService;
    @Autowired
    private TesterFPAService testerFPAService;

@RequestMapping("deleteTestResultFpaById.do")
@ResponseBody
public MyReturn deleteTestResultFpaById(Integer id){

    boolean deleteTesterById = testerFPAService.deleteTesterById(id);
    boolean deleteTestResultFpaById = testResultFpaService.deleteTestResultFpaById(id);
    if (deleteTesterById&&deleteTestResultFpaById){
        return super.getSuccessReturn(myReturn);
    }
    return super.getFailReturn(myReturn);

}



    @RequestMapping("deleteTestResultFpaBatch.do")
    @ResponseBody
    public MyReturn deleteTestResultFpaBatch(String id){
        boolean deleteTestResultFpaBatch = testResultFpaService.deleteTestResultFpaBatch(id);
        boolean deleteTesterBatch = testerFPAService.deleteTesterBatch(id);
        if (deleteTesterBatch&&deleteTestResultFpaBatch){
            return super.getSuccessReturn(myReturn);
        }
        return super.getFailReturn(myReturn);

    }



    /**
     * 接收前台传回的json参数
     *
     */
    @RequestMapping("calScore.do")
    @ResponseBody
    public MyReturn calScore(String res,HttpSession session){
        //转list<map>
        List<Map> maps = JSON.parseArray(res, Map.class);

        Tester tester= (Tester) session.getAttribute("tester");


        //将测试选项结果存入数据库
        boolean b=testResultFpaService.insertTestFpa(maps);
        Integer id = tester.getId();
            //根据测试者id查询测试结果信息
            Tester selectTesterById = testerFPAService.selectTesterById(id);
            //将查询的该测试者的 测试信息存入session中
            session.setAttribute("selectTester",selectTesterById);
           Map map=new HashMap();
           map.put("blue_count",selectTesterById.getBlue_count());
            map.put("red_count",selectTesterById.getRed_count());
            map.put("yellow_count",selectTesterById.getYellow_count());
            map.put("green_count",selectTesterById.getGreen_count());
            //将各种颜色的值 按从大到小的顺序排序
            Map map1 = MapUtil.sortByValue(map, true);
            //取出值最大的，即map第一对键值对
            switch (map1.keySet().iterator().next().toString()){
                case "blue_count":
                    myReturn.setData("/views/character/blue.jsp");
                    break;
                case "red_count" :
                    myReturn.setData("/views/character/red.jsp");
                    break;
                case "yellow_count" :
                    myReturn.setData("/views/character/yellow.jsp");
                    break;
                case "green_count" :
                    myReturn.setData("/views/character/green.jsp");
                    break;
                default:
                    myReturn.setData("/views/character/red.jsp");

            }

        if (b){  return super.getSuccessReturn(myReturn);
        }
        return super.getFailReturn(myReturn);
    }


}
